package com.sjft.cloud.commons.core.resp;

import com.sjft.cloud.commons.core.constant.Constant;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * R
 *
 * @author Jin
 */
@Builder(builderMethodName = "newBuilder")
@ToString
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class R<T> implements Serializable {

    private static final long serialVersionUID = 2L;

    @Getter
    @Setter
    @Builder.Default
    private Integer code = Constant.SUCCESS_CODE;

    @Getter
    @Setter
    @Builder.Default
    private String message = "ok";

    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;



    public static R ok() {
        return R.newBuilder().build();
    }


    public static R error() {
        return R.newBuilder().code(Constant.ERROR_CODE).message("error").build();
    }

    public static R ok(Object data) {
        return R.newBuilder().data(data).build();
    }

    public static R ok(String message, Object data) {
        return R.newBuilder().message(message).data(data).build();
    }


    public static R error(Object data) {
        return R.newBuilder().data(data).code(Constant.ERROR_CODE).message("error").build();
    }

    public static R error(String message, Object data) {
        return R.newBuilder().message(message).data(data).code(Constant.ERROR_CODE).build();
    }
}
